enum messageEnum {
	c2sLoginMID = 1001,
	s2cLoginMID = 1101,
	c2sReadyMID = 1002,
	s2cReadyMID = 1102,
	s2cGameStartMID = 1103,
	s2cRoundEndMID = 1104,
	c2sSendEnemyMID = 1005,
	s2cSendEnemyMID = 1105,
	c2sGameoverMID = 1006,
	s2cGameoverMID = 1106,
	c2sleakEnemyMID = 1007,
	s2cleakEnemyMID = 1107,
	c2sskillMID = 1008,
	s2cskillMID = 1108,
	s2cgetGold = 1109
}
class webNetworkMananger {
	public isConnecteed: boolean;
	public constructor() {
	}
	/**心跳 */
	private hertBeatTimer: egret.Timer;
	public static instance: webNetworkMananger;
	public static getInstance(): webNetworkMananger {
		if (this.instance == null) {
			this.instance = new webNetworkMananger();
		}
		return this.instance;
	}
	private heartTimer() {
		// this.hertBeatTimer = new egret.Timer(1000, -1);
		// this.hertBeatTimer.addEventListener(egret.TimerEvent.TIMER, e => {
		// 	if (this.isConnecteed == true)
		// 		webNetworkMananger.getInstance().SendMeesage('hert', {});
		// }, this);
		// this.hertBeatTimer.start();
	}
	//用来接受游戏场景的
	public gameSceneRec: any;

	public static test: boolean = false;

	/**服务器移动 */
	private _root_url: string = "176.122.156.95";
	//private _root_url: string = "172.16.19.190";
	//private _root_url: string = "172.16.19.104";
	public static isServerMove: boolean = false;

	public webSocket: egret.WebSocket;
	private heartMessag: egret.ByteArray;
	//CONCNET SERVER
	public connect(): void {
		if (this._root_url === null) {
			console.error("未设置连接HOST IP");
			return;
		}
		let heartMessagetmp = new awesomepackage.AwesomeMessage();
		heartMessagetmp.meesageId = 51;
		this.heartMessag = new egret.ByteArray(awesomepackage.AwesomeMessage.encode(heartMessagetmp).finish());
		// if (this.isConnecteed === true) {
		// 	console.error("服务器已经连接");
		// 	let m_AwesomeMessage = new awesomepackage.AwesomeMessage();
		// 	let m_c2sEnterRoom = new awesomepackage.c2sEnterRoom();
		// 	m_c2sEnterRoom.account = DataProl.account;
		// 	m_c2sEnterRoom.deessUp = JSON.stringify(DataProl.userCurrentDressUp);
		// 	m_AwesomeMessage.meesageId = 1002;
		// 	m_AwesomeMessage.data = awesomepackage.c2sEnterRoom.encode(m_c2sEnterRoom).finish();
		// 	var buffer =awesomepackage.AwesomeMessage.encode(m_AwesomeMessage).finish();
		// 	var byte2: egret.ByteArray = new egret.ByteArray(buffer);
		// 	webNetworkMananger.getInstance().SendBitMeesage(byte2);
		// 	return;
		// }
		this.webSocket = new egret.WebSocket();
		//设置数据格式为二进制，默认为字符串
		this.webSocket.type = egret.WebSocket.TYPE_BINARY;
		//添加收到数据侦听，收到数据会调用此方法
		this.webSocket.addEventListener(egret.ProgressEvent.SOCKET_DATA, this.onReceiveMessage, this);
		//添加链接打开侦听，连接成功会调用此方法
		this.webSocket.addEventListener(egret.Event.CONNECT, this.onSocketOpen, this);
		//添加链接关闭侦听，手动关闭或者服务器关闭连接会调用此方法
		this.webSocket.addEventListener(egret.Event.CLOSE, this.onSocketClose, this);
		//添加异常侦听，出现异常会调用此方法
		this.webSocket.addEventListener(egret.IOErrorEvent.IO_ERROR, this.onSocketError, this);
		//连接服务器
		this.webSocket.connect(this._root_url, 3011);
	}


	public onSocketOpen(): void {
		this.isConnecteed = true;
		//	LoginMain.LoginBackMess=0;
		console.log("连接成功");
	}
	public onSocketClose(): void {
		this.isConnecteed = false;
		console.log("连接失败");
	}
	public onSocketError(): void {
		console.log("连接报错");
	}
	public SendBitMeesage(mes: egret.ByteArray): void {
		this.webSocket.writeBytes(mes);
		this.webSocket.flush();
	}
	//recive message
	public onReceiveMessage(e: egret.Event): void {
		var byte: egret.ByteArray = new egret.ByteArray();
		this.webSocket.readBytes(byte);
		this.reciveMessageHandle(byte.bytes);
	}
	/**显示错误信息 */
	public showErrorMessage(data: any) {
		this.gameSceneRec.displayErrorMessage(data);
	}
	/**登录 */
	public LoginGameServer() {
		//SendBitMeesage
		let m_AwesomeMessage = new awesomepackage.AwesomeMessage();
		let m_c2sLogin = new awesomepackage.c2sLogin();
		m_c2sLogin.account = "Wayne";
		console.log(m_c2sLogin.account);
		if (messageManage.avatarUrl != null)
			m_c2sLogin.headimg = messageManage.avatarUrl;
		m_c2sLogin.sign = "666";
		m_c2sLogin.sex = "sex";
		m_c2sLogin.name = "name";
		m_AwesomeMessage.meesageId = messageEnum.c2sLoginMID;
		m_AwesomeMessage.data = awesomepackage.c2sLogin.encode(m_c2sLogin).finish();
		var buffer = awesomepackage.AwesomeMessage.encode(m_AwesomeMessage).finish();
		var byte2: egret.ByteArray = new egret.ByteArray(buffer);
		webNetworkMananger.getInstance().SendBitMeesage(byte2);
	}
	/**准备 */
	public user_ready() {
		let m_AwesomeMessage = new awesomepackage.AwesomeMessage();
		let m_c2sReady = new awesomepackage.c2sReady();
		m_c2sReady.userID = messageManage.userid;
		m_AwesomeMessage.meesageId = messageEnum.c2sReadyMID;
		m_AwesomeMessage.data = awesomepackage.c2sReady.encode(m_c2sReady).finish();
		var buffer = awesomepackage.AwesomeMessage.encode(m_AwesomeMessage).finish();
		var byte2: egret.ByteArray = new egret.ByteArray(buffer);
		webNetworkMananger.getInstance().SendBitMeesage(byte2);

	}
	/**发送敌机 */
	public send_enemy(roundNumber: any) {
		let m_AwesomeMessage = new awesomepackage.AwesomeMessage();
		let m_c2sSendEnemy = new awesomepackage.c2sSendEnemy();
		m_c2sSendEnemy.enmeys = GameUtil.PlayerMessage.instance.sendEnemy;
		GameUtil.PlayerMessage.instance.sendEnemy = [];
		m_c2sSendEnemy.userId = messageManage.userid;
		m_c2sSendEnemy.Roundnumber = roundNumber;
		/**发送剩余金币 */
		m_c2sSendEnemy.gold = GameUtil.PlayerMessage.instance.money;
		m_AwesomeMessage.meesageId = messageEnum.c2sSendEnemyMID;
		m_AwesomeMessage.data = awesomepackage.c2sSendEnemy.encode(m_c2sSendEnemy).finish();
		var buffer = awesomepackage.AwesomeMessage.encode(m_AwesomeMessage).finish();
		var byte2: egret.ByteArray = new egret.ByteArray(buffer);
		webNetworkMananger.getInstance().SendBitMeesage(byte2);
	}
	/**发送游戏结束 */
	public gameOver() {
		let m_AwesomeMessage = new awesomepackage.AwesomeMessage();
		let m_c2sGameover = new awesomepackage.c2sGameover();
		m_c2sGameover.userId = messageManage.userid;
		m_AwesomeMessage.meesageId = messageEnum.c2sGameoverMID;
		m_AwesomeMessage.data = awesomepackage.c2sGameover.encode(m_c2sGameover).finish();
		var buffer = awesomepackage.AwesomeMessage.encode(m_AwesomeMessage).finish();
		var byte2: egret.ByteArray = new egret.ByteArray(buffer);
		webNetworkMananger.getInstance().SendBitMeesage(byte2);
	}
	/**发送自己漏兵给对方 */
	public send_leakEnemy() {
		let m_AwesomeMessage = new awesomepackage.AwesomeMessage();
		let m_c2sleakEnemy = new awesomepackage.c2sleakEnemy();
		//预留自己漏兵变量，更改null即可
		var leakEnemy = GameUtil.PlayerMessage.instance.Game2addEnemy;
		m_c2sleakEnemy.userId = messageManage.userid;
		m_c2sleakEnemy.enmeys = leakEnemy;
		/**发送金币 */
		m_c2sleakEnemy.gold = GameUtil.PlayerMessage.instance.money;
		m_AwesomeMessage.meesageId = messageEnum.c2sleakEnemyMID;
		m_AwesomeMessage.data = awesomepackage.c2sleakEnemy.encode(m_c2sleakEnemy).finish();
		var buffer = awesomepackage.AwesomeMessage.encode(m_AwesomeMessage).finish();
		var byte2: egret.ByteArray = new egret.ByteArray(buffer);
		webNetworkMananger.getInstance().SendBitMeesage(byte2);
	}
	/**发送自己技能和堡垒hp给对方
		 * 0-技能
		 * 1-堡垒hp
		 */
	public send_skill(type: number, No: number) {
		let m_AwesomeMessage = new awesomepackage.AwesomeMessage();
		let m_c2sskill = new awesomepackage.c2sskill();
		m_c2sskill.userId = messageManage.userid;
		m_c2sskill.type = type;
		m_c2sskill.skill = No;
		m_AwesomeMessage.meesageId = messageEnum.c2sskillMID;
		m_AwesomeMessage.data = awesomepackage.c2sskill.encode(m_c2sskill).finish();
		var buffer = awesomepackage.AwesomeMessage.encode(m_AwesomeMessage).finish();
		var byte2: egret.ByteArray = new egret.ByteArray(buffer);
		webNetworkMananger.getInstance().SendBitMeesage(byte2);
	}
	/**加载对方微信头像 */
	private getWxHead(data) {
		Main.EnemyHead = new egret.Bitmap();
		Main.EnemyHead.texture = data;
	}
	/**接收服务器数据 */
	public reciveMessageHandle(backData: any) {
		var pMessaage = awesomepackage.AwesomeMessage.decode(backData);
		console.log(pMessaage.meesageId);
		/**登录 */
		if (pMessaage.meesageId == messageEnum.s2cLoginMID) {
			let tmpMessage = awesomepackage.s2cLogin.decode(pMessaage.data);
			/**飞机配置文件 */
			let enmeysConfig = tmpMessage.enmeysConfig;

			//存到客户端
			GameUtil.PlayerMessage.instance.planeConfig = enmeysConfig;
			Fighter.StartUI.StartUI_sole.skip_oline_Main(tmpMessage);

			return;
		}
		/**准备 */
		if (pMessaage.meesageId == messageEnum.s2cReadyMID) {
			let tmpMessage = awesomepackage.s2cReady.decode(pMessaage.data);
			Fighter.OlUI.OlUI_solo.ready_result(tmpMessage);
			return;
		}
		/**开始 */
		if (pMessaage.meesageId == messageEnum.s2cGameStartMID) {
			//初始化回合数
			messageManage.roundNumber = 0;
			//用户信息
			let tmpMessage = awesomepackage.s2cGameStart.decode(pMessaage.data);
			if (tmpMessage.headimg != null) {
				//将玩家头像信息添加到预加载中
				RES.getResByUrl(tmpMessage.headimg, this.getWxHead, this, "image");
				console.log("敌方微信头像：" + tmpMessage.headimg);

			}
			Fighter.OlUI.OlUI_solo.marry_result(tmpMessage);



			return;
		}

		if (pMessaage.meesageId == 50) {
			//初始化回合数

			webNetworkMananger.getInstance().SendBitMeesage(this.heartMessag);
			return;
		}
		//接收发兵请求
		if (pMessaage.meesageId == messageEnum.s2cRoundEndMID) {
			let tmpMessage = awesomepackage.s2cRoundEnd.decode(pMessaage.data);
			if (tmpMessage.errorCode == 0) {
				webNetworkMananger.getInstance().send_enemy(tmpMessage.Roundnumber);
			}
			return;
		}
		//接收对方发兵
		if (pMessaage.meesageId == messageEnum.s2cSendEnemyMID) {
			let tmpMessage = awesomepackage.s2cSendEnemy.decode(pMessaage.data);
			/**对方发兵 */
			let enemys = tmpMessage.enmeys;
			/**对方发兵位置 */
			let enemys_positions = tmpMessage.enmeysPosition;

			GameUtil.PlayerMessage.instance.getEnemy = enemys;
			GameUtil.PlayerMessage.instance.airBattlePosition = tmpMessage.enmeysPosition;
			Fighter.AirBattle.startCreatePlane = true;

			//切换战斗场景
			if (GameUtil.PlayerMessage.instance.game_turns != 1) {

				Scene.SceneManager.SceneManager_sole.scene_prepare.EndPrepare = true;

			}
			return;
		}
		//战斗结果
		//0-输 1-赢
		if (pMessaage.meesageId == messageEnum.s2cGameoverMID) {
			let tmpMessage = awesomepackage.s2cGameover.decode(pMessaage.data);
			var whoWin = tmpMessage.whoWin;
			//发送游戏结束信息
			Scene.SceneManager.SceneManager_sole.winNum = whoWin;
			Scene.SceneManager.SceneManager_sole.is_gameOver = true;
			return;
		}
		//接收对方漏兵
		if (pMessaage.meesageId == messageEnum.s2cleakEnemyMID) {
			let tmpMessage = awesomepackage.s2cleakEnemy.decode(pMessaage.data);
			/**漏兵 */
			var cleakEnemy = tmpMessage.enmeys;
			var userID = tmpMessage.userId;
			/**漏兵位置 */
			var enemys_positions = tmpMessage.enmeysPosition;
			//自己漏的兵
			if (userID == messageManage.userid) {
				GameUtil.PlayerMessage.instance.Game2addEnemy = cleakEnemy;
				GameUtil.PlayerMessage.instance.defensePosition = enemys_positions;
				Fighter.Defense.startCreatPlane = true;
				Fighter.Defense.isOver = true;
			}
			//对方漏的兵
			else {

				GameUtil.PlayerMessage.instance.EneMissList = cleakEnemy;
				GameUtil.PlayerMessage.instance.enemyDefensePosition = enemys_positions;
				Fighter.EnemyDefense.startCreatePlane = true;
				Fighter.EnemyDefense.Is_Over = true;
			}

			return;
		}
		//接收对方技能和堡垒hp
		if (pMessaage.meesageId == messageEnum.s2cskillMID) {
			let tmpMessage = awesomepackage.s2cskill.decode(pMessaage.data);
			var type = tmpMessage.type
			var No = tmpMessage.skill;
			if (type == 0) {
				Fighter.EnemyDefenseUI.EnemyHomeHp = No;
			}
			if (type == 1) {
				Fighter.EnemyDefense.screenAtk = true;
				Fighter.EnemyDefenseUI.PlayAnimation = true;
			}
			return;
		}
		//接收金币
		if (pMessaage.meesageId == messageEnum.s2cgetGold) {
			let tmpMessage = awesomepackage.s2cgetGold.decode(pMessaage.data);
			let myGold = tmpMessage.gold;
			GameUtil.PlayerMessage.instance.money = myGold;
			return;
		}

		/**提示错误信息 */
		if (backData.cmdName == "300") {

			var terr = awesomepackage.s2cErroMessage.decode(pMessaage.data);
			if (this.gameSceneRec != null)
				this.showErrorMessage(terr);
			return;
		}





	}



}